package com.dbsop.domain.rest;

import com.dbsop.common.enums.Order;
import com.dbsop.domain.EsAggCondition;
import com.dbsop.domain.agg.Group;
import com.dbsop.domain.rest.agg.*;
import com.dbsop.domain.rest.info.RFFM;

/**
 * Es 聚合操作
 */
public class EsAggRestCondition extends EsAggCondition {

    public Group Group(String fieldName){
        group = new RestTermGroup(fieldName);
        return group;
    }

    public Group Group(String fieldName, String formatName){
        group = new RestTermGroup(fieldName,formatName);
        return group;
    }

    public EsAggCondition group(String fieldName){
        group = new RestTermGroup(fieldName);
        return this;
    }

    public EsAggCondition group(String fieldName, String formatName){
        group = new RestTermGroup(fieldName,formatName);
        return this;
    }


    public Group RangeGroup(String fieldName, RFFM... rffms){
        RangeGroup(fieldName,fieldName,rffms);
        return group;
    }

    public Group RangeGroup(String fieldName, String formatName,RFFM... rffms){
        RestScopeGroup scopeGroup = new RestScopeGroup(fieldName,formatName);
        for (RFFM rffm : rffms) {
            scopeGroup.add(rffm);
        }
        group =  scopeGroup;
        return group;
    }

    public EsAggCondition rangeGroup(String fieldName, RFFM... rffms){
        rangeGroup(fieldName,fieldName,rffms);
        return this;
    }

    public EsAggCondition rangeGroup(String fieldName, String formatName, RFFM... rffms){
        RestScopeGroup scopeGroup = new RestScopeGroup(fieldName,formatName);
        for (RFFM rffm : rffms) {
            scopeGroup.add(rffm);
        }
        group =  scopeGroup;
        return this;
    }



    public EsAggCondition Sum(String fieldName){
        add(new RestSum(fieldName));
        return this;
    }

    public EsAggCondition Avg(String fieldName){
        add(new RestAvg(fieldName));
        return this;
    }
    public EsAggCondition Count(String fieldName){
        add(new RestCount(fieldName));
        return this;
    }
    public EsAggCondition Max(String fieldName, String... fields){
        add(new RestMax(fieldName));
        return this;
    }

    public EsAggCondition MaxExtFields(String fieldName, String... fields){
        MaxExtFields(fieldName,1,fields);
        return this;
    }

    public EsAggCondition MaxExtFields(String fieldName, int num, String... fields){
        TopHits topHits = new TopHits(fieldName, Order.DESC,num);
        if (fields != null) {
            for (String field : fields) {
                topHits.addField(field);
            }
        }
        topHits.addField(fieldName);
        add(topHits);
        return this;
    }


    public EsAggCondition Min(String fieldName, String... fields){
        add(new RestMin(fieldName));
        return this;
    }
    public EsAggCondition MinExtFields(String fieldName, String... fields){
        MinExtFields(fieldName,1,fields);
        return this;
    }

    public EsAggCondition MinExtFields(String fieldName, int num, String... fields){
        TopHits topHits = new TopHits(fieldName, Order.ASC,num);
        if (fields != null) {
            for (String field : fields) {
                topHits.addField(field);
            }
        }
        topHits.addField(fieldName);
        add(topHits);
        return this;
    }



    public EsAggCondition Sum(String fieldName, String formatName){
        add(new RestSum(fieldName,formatName));
        return this;
    }

    public EsAggCondition Avg(String fieldName, String formatName){
        add(new RestAvg(fieldName,formatName));
        return this;
    }
    public EsAggCondition Count(String fieldName, String formatName){
        add(new RestCount(fieldName,formatName));
        return this;
    }
    public EsAggCondition Max(String fieldName, String formatName){
        add(new RestMax(fieldName,formatName));
        return this;
    }

    public EsAggCondition Min(String fieldName, String formatName){
        add(new RestMin(fieldName,formatName));
        return this;
    }



}
